/*
 * 
 * 
 * 
 */
package com.hboxs.asl.controller.admin;

import com.hboxs.asl.Message;
import com.hboxs.asl.Pageable;
import com.hboxs.asl.entity.ReturnsRecord;
import com.hboxs.asl.service.ReturnsRecordService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;

/**
 * Controller - 退货记录
 */
@Controller("adminReturnsRecordController")
@RequestMapping("/asl-admin/returns_record")
public class ReturnsRecordController extends BaseController {

    @Resource(name = "returnsRecordServiceImpl")
    private ReturnsRecordService returnsRecordService;

    /**
     * 添加
     */
    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String add(ModelMap model) {
        model.addAttribute("reasons", ReturnsRecord.Reason.values());
        model.addAttribute("status", ReturnsRecord.Status.values());
        return "/admin/returns_record/add";
    }

    /**
     * 保存
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public String save(ReturnsRecord returnsRecord, RedirectAttributes redirectAttributes) {
        if (!isValid(returnsRecord)) {
            return ERROR_VIEW;
        }
        returnsRecordService.save(returnsRecord);
        addFlashMessage(redirectAttributes, SUCCESS_MESSAGE);
        return "redirect:list.htm";
    }

    /**
     * 编辑
     */
    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    public String edit(Long id, ModelMap model) {
        model.addAttribute("returnsRecord", returnsRecordService.find(id));
        return "/admin/returns_record/edit";
    }

    /**
     * 更新
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public String update(ReturnsRecord returnsRecord, RedirectAttributes redirectAttributes) {
        if (!isValid(returnsRecord)) {
            return ERROR_VIEW;
        }
        returnsRecordService.update(returnsRecord, "");
        addFlashMessage(redirectAttributes, SUCCESS_MESSAGE);
        return "redirect:list.htm";
    }

    /**
     * 列表
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String list(ReturnsRecord.Reason reason, ReturnsRecord.Status status, Pageable pageable, ModelMap model) {
        model.addAttribute("reason", reason);
        model.addAttribute("status", status);
        model.addAttribute("page", returnsRecordService.findPage(reason, status, pageable));
        return "/admin/returns_record/list";
    }

    /**
     * 删除
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public
    @ResponseBody
    Message delete(Long[] ids) {
        returnsRecordService.delete(ids);
        return SUCCESS_MESSAGE;
    }


    /**
     * 查看
     * @param id
     * @param model
     * @return
     */
    @RequestMapping(value = "/view", method = RequestMethod.GET)
    public String view(Long id, ModelMap model) {
        model.addAttribute("ReturnsRecordReason", ReturnsRecord.Reason.values());
        model.addAttribute("ReturnsRecordStatus", ReturnsRecord.Status.values());
        model.addAttribute("returns_record", returnsRecordService.find(id));
        return "/admin/returns_record/view";
    }

    /**
     * 拒绝申请
     * @param id
     * @param redirectAttributes
     * @return
     */
    @RequestMapping(value = "/reject", method = RequestMethod.POST)
    public String reject(Long  id,RedirectAttributes redirectAttributes) {
        ReturnsRecord returnsRecord = returnsRecordService.find(id);
        if (!isValid(returnsRecord)){
            return ERROR_VIEW;
        }
        if (returnsRecord.getStatus() == ReturnsRecord.Status.apply){
            returnsRecord.setStatus(ReturnsRecord.Status.reject);
            returnsRecordService.update(returnsRecord);
        }
        addFlashMessage(redirectAttributes, SUCCESS_MESSAGE);
        return "redirect:list.htm";
    }

    /**
     * 申请成功
     * @param id
     * @param redirectAttributes
     * @return
     */
    @RequestMapping(value = "/deal", method = RequestMethod.POST)
    public String deal(Long id, RedirectAttributes redirectAttributes) {
        ReturnsRecord returnsRecord = returnsRecordService.find(id);
        if (!isValid(returnsRecord)){
            return ERROR_VIEW;
        }
        if (returnsRecord.getStatus() == ReturnsRecord.Status.apply){
            returnsRecord.setStatus(ReturnsRecord.Status.deal);
            returnsRecordService.update(returnsRecord);
        }
        addFlashMessage(redirectAttributes, SUCCESS_MESSAGE);
        return "redirect:list.htm";
    }

}
